{% extends 'admin_dashboard.html' %}
{% block content %}
<div class="card">
  <h2>编辑银行卡</h2>
  <form id="editCardForm" method="POST">
    <div class="form-group">
      <label>卡号</label>
      <input type="text" name="cardID" value="{{ card.cardID }}" class="form-control" readonly disabled>
    </div>

    <div class="form-group">
      <label>存款类型</label>
      <input type="text" name="curID" value="{{ card.curID }}" class="form-control" required>
    </div>

    <div class="form-group">
      <label>开户日期</label>
      <input type="date" name="openDate" value="{{ card.openDate }}" class="form-control" readonly disabled>
    </div>

    <div class="form-group">
      <label>开户金额</label>
      <input type="number" name="openMoney" value="{{ card.openMoney }}" class="form-control" step="0.01" readonly disabled>
    </div>

    <div class="form-group">
      <label>当前余额</label>
      <input type="number" name="balance" value="{{ card.balance }}" class="form-control" step="0.01" required>
    </div>

    <div class="form-group">
      <label>密码</label>
      <input type="text" name="pass" value="{{ card.pass }}" class="form-control" required>
    </div>

    <div class="form-group">
      <label>是否挂失</label>
      <input type="checkbox" name="IsReportLoss" {% if card.IsReportLoss == '是' %}checked{% endif %}>
    </div>

    <div class="form-group">
      <label>所属客户编号</label>
      <select name="customerID" class="form-control" required>
        {% for c in customers %}
        <option value="{{ c.customerID }}" {% if card.customerID == c.customerID %}selected{% endif %}>{{ c.customerID }} - {{ c.customerName }}</option>
        {% endfor %}
      </select>
    </div>

    <button type="submit" class="btn btn-primary">保存修改</button>
    <a href="{{ url_for('cards') }}" class="btn btn-secondary">返回</a>
  </form>
</div>

<script>
document.getElementById('editCardForm').addEventListener('submit', function(e) {
  e.preventDefault();
  const formData = new FormData(this);
  fetch(window.location.href, {
    method: 'POST',
    body: formData
  })
  .then(res => res.json())
  .then(data => {
    if (data.success) {
      alert("✅ 修改成功！");
      window.location.href = "{{ url_for('cards') }}";
    } else {
      alert("❌ " + data.message);
    }
  })
  .catch(err => {
    alert("⚠️ 请求失败，请稍后再试");
    console.error(err);
  });
});
</script>
{% endblock %}
